﻿using StartEAS_API.Utils;
using StartsEAS_API.DAL.DALTools;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.ClassD
{
    public class ClassDAL
    {
        private readonly StarsEASContext _context;
        public ClassDAL(StarsEASContext context)
        {
            _context = context;
        }
        public Object GetClassesByClassId(string ClassId)
        {
            StarsEASContext _context = new StarsEASContext();
            var classes = from cls in _context.Classes
                          join Professiona in _context.Professionals on cls.ProfessionalId equals Professiona.ProfessionalId
                          join College in _context.Colleges on cls.CollegeId equals College.CollegeId
                          join Worker in _context.Workers on cls.WorkerId equals Worker.WorkerId
                          where cls.ClassId == ClassId
                          select new
                          {
                              Id = cls.Id,
                              ClassId = cls.ClassId,
                              CollegeId = cls.CollegeId,
                              CollegeName = College.CollegeName,
                              ProfessionalId = cls.ProfessionalId,
                              ProfessionalName = Professiona.ProfessionalName,
                              Grade = cls.Grade,
                              WorkerId = cls.WorkerId,
                              WorkerName = Worker.WorkerName
                          };
            AppLog.WriteInfo($"查询班级信息---查询班级{ClassId}的班级信息", true);
            return classes;
        }

        public Object GetClassesByToken(string token) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询班级信息---账户{token}尝试登录但登录失败", true);
                return null;
            }
            if (token.IsStudent(_context))
            {
                Model.Models.Student student = token.GetStudentByToken(_context);
                var classes = from cls in _context.Classes
                              join Professiona in _context.Professionals on cls.ProfessionalId equals Professiona.ProfessionalId
                              join College in _context.Colleges on cls.CollegeId equals College.CollegeId
                              join Worker in _context.Workers on cls.WorkerId equals Worker.WorkerId
                              where cls.ClassId == student.ClassId
                              select new
                              {
                                  Id = cls.Id,
                                  ClassId = cls.ClassId,
                                  CollegeId = cls.CollegeId,
                                  CollegeName = College.CollegeName,
                                  ProfessionalId = cls.ProfessionalId,
                                  ProfessionalName = Professiona.ProfessionalName,
                                  Grade = cls.Grade,
                                  WorkerId = cls.WorkerId,
                                  WorkerName = Worker.WorkerName
                              };
                AppLog.WriteInfo($"查询班级信息---{student.StuName}学生查询班级信息", true);
                return classes;
            }
            //老师
            else {
                Worker worker = token.GetWorkerByToken(_context);
                var classes = from cls in _context.Classes
                              join Professiona in _context.Professionals on cls.ProfessionalId equals Professiona.ProfessionalId
                              join College in _context.Colleges on cls.CollegeId equals College.CollegeId
                              join Worker in _context.Workers on cls.WorkerId equals Worker.WorkerId
                              where Worker.WorkerId == worker.WorkerId
                              select new
                              {
                                  Id = cls.Id,
                                  ClassId = cls.ClassId,
                                  CollegeId = cls.CollegeId,
                                  CollegeName = College.CollegeName,
                                  ProfessionalId = cls.ProfessionalId,
                                  ProfessionalName = Professiona.ProfessionalName,
                                  Grade = cls.Grade,
                                  WorkerId = cls.WorkerId,
                                  WorkerName = Worker.WorkerName
                              };
                AppLog.WriteInfo($"查询班级信息---{worker.WorkerName}老师查询班级信息", true);
                return classes;
            }
        }

        public int AddClasses(string token, Class classes) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.FirstOrDefault(u => u.Token == token);
            if (user == null)
            {
                AppLog.WriteError($"新增班级信息---账户{token}尝试登录但登录失败", true);
                return -1;
            }
            if (user.PositionId!=6)
            {
                AppLog.WriteError($"新增班级信息---账户{user.Username}非教务处人员尝试新增班级信息", true);
                return -1;
            }
            if (classes == null)
            {
                AppLog.WriteError($"新增班级信息---账户{user.Username}未携带班级信息尝试新增班级信息", true);
                return -1;
            }
            classes.Id = 0;
            _context.Classes.Add(classes);
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"新增班级信息---账户{user.Username}尝试新增班级信息但新增失败", true);
                return -1;
            }
            AppLog.WriteInfo($"新增班级信息---账户{user.Username}尝试新增班级信息操作成功", true);
            return result;
        }

        public int UpdateClasses(string token, Class data) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.FirstOrDefault(u => u.Token == token);
            if (user == null)
            {
                AppLog.WriteError($"修改班级信息---账户{token}尝试登录但登录失败", true);
                return -1;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"修改班级信息---账户{user.Username}尝试使用非教务处账户修改班级信息", true);
                return -1;
            }
            if (data == null)
            {
                AppLog.WriteError($"修改班级信息---账户{token}尝试使用空数据修改班级信息", true);
                return -1;
            }
            Class classes = _context.Classes.Find(data.Id);
            if (classes == null)
            {
                AppLog.WriteError($"修改班级信息---账户{token}未查询到编号{data.Id}班级", true);
                return -1;
            }
            classes.CollegeId = data.CollegeId;
            classes.ClassId = data.ClassId;
            classes.ProfessionalId = data.ProfessionalId;
            classes.Grade = data.Grade;
            classes.WorkerId = data.WorkerId;
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"修改班级信息---账户{user.Username}尝试修改班级信息但修改失败", true);
                return -1;
            }
            AppLog.WriteInfo($"修改班级信息---账户{user.Username}尝试修改班级信息操作成功", true);
            return result;
        }

        public IEnumerable<Class> GetAllClasses() {
            StarsEASContext _context = new StarsEASContext();
            IEnumerable<Class> classes = _context.Classes.ToList();
            AppLog.WriteInfo($"查询班级信息---查询班级所有班级信息", true);
            return classes;
        }

        public IEnumerable<Class> GetAllClassesByProfessionalId(int ProfessionalId)
        {
            StarsEASContext _context = new StarsEASContext();
            IEnumerable<Class> classes = _context.Classes.Where(c=>c.ProfessionalId== ProfessionalId).ToList();
            AppLog.WriteInfo($"查询班级信息---查询班级所有班级信息", true);
            return classes;
        }
    }
}
